package com.sky.service.impl;

import com.alibaba.dashscope.app.Application;
import com.alibaba.dashscope.app.ApplicationParam;
import com.alibaba.dashscope.app.ApplicationResult;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.sky.service.AIService;
import io.reactivex.Flowable;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * 通义千问AI服务实现类
 */
@Service
@Slf4j
public class AIServiceImpl implements AIService {

    @Value("${aliyun.dashscope.api.key}")
    private String apiKey;

    @Value("${aliyun.dashscope.app.id}")
    private String appId;

    @Override
    public Flowable<ApplicationResult> chat(String prompt) {
        try {
            ApplicationParam param = ApplicationParam.builder()
                    .apiKey(apiKey)
                    .appId(appId)
                    .prompt(prompt)
                    .incrementalOutput(true)
                    .build();
            
            Application application = new Application();
            return application.streamCall(param);
        } catch (NoApiKeyException | InputRequiredException e) {
            log.error("调用通义千问流式API失败", e);
            throw new RuntimeException("AI服务调用失败", e);
        }
    }
} 